package com.eshop;

import com.eshop.annotation.AnonymousAccess;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import com.eshop.utils.SpringContextHolder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.binarywang.spring.starter.wxjava.miniapp.config.WxMaAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@EnableAsync
@RestController
@SpringBootApplication(exclude = { WxMaAutoConfiguration.class })
@EnableTransactionManagement
@EnableScheduling
@MapperScan(basePackages = { "com.eshop.modules.*.service.mapper", "com.eshop.config" })
public class AdminRun extends SpringBootServletInitializer
{
    protected SpringApplicationBuilder configure(final SpringApplicationBuilder builder) {
        return builder.sources(new Class[] { AdminRun.class });
    }
    
    public static void main(final String[] args) {
        SpringApplication.run(AdminRun.class, args);
        System.out.println("--------");
    }
    
    @Bean
    public SpringContextHolder springContextHolder() {
        return new SpringContextHolder();
    }

    @Bean
    public ServletWebServerFactory webServerFactory() {
        final TomcatServletWebServerFactory fa = new TomcatServletWebServerFactory();
        fa.addConnectorCustomizers(new TomcatConnectorCustomizer[] { connector -> connector.setProperty("relaxedQueryChars", "[]{}") });
        return (ServletWebServerFactory)fa;
    }
    
    @GetMapping({ "/" })
    @AnonymousAccess
    public String index() {
        return "Backend service started successfully";
    }
}
